package com.itheima.hmshopping.dubbo.apiImpl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.hmshopping.dto.OrderListDto;
import com.itheima.hmshopping.dubbo.api.OmsOrderServiceApi;
import com.itheima.hmshopping.dubbo.mapper.OmsOrderMapper;
import com.itheima.hmshopping.entity.OmsOrder;
import com.itheima.hmshopping.util.HmPage;
import org.apache.commons.lang.StringUtils;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;
import java.util.stream.Collectors;

@DubboService
public class OmsOrderServiceApiImpl implements OmsOrderServiceApi {
    @Autowired
    private OmsOrderMapper omsOrderMapper;

    /**
     * 查询订单
     *
     * @param orderListDto
     * @return
     */
    @Override
    public HmPage<OmsOrder> getOrderList(OrderListDto orderListDto) {
        LambdaQueryWrapper<OmsOrder> lqw = new LambdaQueryWrapper<>();
        lqw.like(StringUtils.isNotEmpty(orderListDto.getOrderSn()), OmsOrder::getOrderSn, orderListDto.getOrderSn());             //订单编号
        lqw.like(StringUtils.isNotEmpty(orderListDto.getCreateTime()), OmsOrder::getCreateTime, orderListDto.getCreateTime());    //订单提交时间
        lqw.eq(ObjectUtil.isNotEmpty(orderListDto.getOrderType()), OmsOrder::getOrderType, orderListDto.getOrderType());          //订单类型

        lqw.like(StringUtils.isNotEmpty(orderListDto.getReceiverKeyword()), OmsOrder::getReceiverName, orderListDto.getReceiverKeyword());    //订单关键词
        lqw.like(StringUtils.isNotEmpty(orderListDto.getReceiverKeyword()), OmsOrder::getReceiverPhone, orderListDto.getReceiverKeyword());

        lqw.eq(ObjectUtil.isNotEmpty(orderListDto.getSourceType()), OmsOrder::getSourceType, orderListDto.getSourceType());       //订单来源
        lqw.eq(ObjectUtil.isNotEmpty(orderListDto.getStatus()), OmsOrder::getStatus, orderListDto.getStatus());                   //订单状态

        Page<OmsOrder> page = new Page<>(orderListDto.getPageNum(),orderListDto.getPageSize());

        Page<OmsOrder> omsOrderPage = omsOrderMapper.selectPage(page, lqw);

        //对象拷贝
        HmPage<OmsOrder> hmPage=new HmPage<>();
        BeanUtil.copyProperties(omsOrderPage,hmPage);
        return hmPage;
    }

    /**
     * 批量删除订单
     * @param ids
     */
    @Override
    public void deleteOrderByIds(Integer[] ids) {
        //获取要修改的订单集合
        LambdaQueryWrapper<OmsOrder> lqw=new LambdaQueryWrapper<>();
        lqw.in(OmsOrder::getId,ids);
        List<OmsOrder> omsOrderList = omsOrderMapper.selectList(lqw);

        //修改对象的Deletestatu属性为1,完成更新操作
        omsOrderList.stream().map(omsOrder -> {
            omsOrder.setDeleteStatus(1);
            omsOrderMapper.updateById(omsOrder);
            return omsOrder;
        }).collect(Collectors.toList());
    }
}
